package com.ymk.cmd;

import com.ymk.base.cmd.ACmd;
import com.ymk.base.cmd.CmdSign;
import com.ymk.base.utlis.ParamsBuilder;
import com.ymk.bo.orders.OrdersBO;
import com.ymk.core.code.cmd.OrdersCmdCode;
import com.ymk.service.IOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Service
public class OrdersGetCmd extends ACmd {

    @Override
    public CmdSign executeMop(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        OrdersBO reqData = super.getData(sign, OrdersBO.class);

        ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
                .addParam("id", reqData.getId())
                .addParam("loadItem", true)
                .addParam("loadDesk", true)
                .setReturnStruct(sign.getReturnStruct());
        OrdersBO respData = this.ordersService.get(paramsBuilder.build());
        respSign.setSource(super.getSource(respData));

        return respSign;
    }

    @Override
    public CmdSign executeStore(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        OrdersBO reqData = super.getData(sign, OrdersBO.class);

        ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
                .addParam("id", reqData.getId())
                .addParam("loadItem", true)
                .addParam("loadDesk", true)
                .setReturnStruct(sign.getReturnStruct());
        OrdersBO respData = this.ordersService.get(paramsBuilder.build());

        respSign.setSource(super.getSource(respData));
        return respSign;
    }

    @Override
    public CmdSign executeServer(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        Map param = super.getData(sign, Map.class);

        OrdersBO respData = this.ordersService.get(param);

        respSign.setSource(super.getSource(respData));
        return respSign;
    }

    @Override
    public String getCmdCode() {
        return OrdersCmdCode.ORDERS_GET;
    }

    @Autowired
    private IOrdersService ordersService;
}